const { Service } = require("egg");

class BwlService extends Service {
    /**
     * 添加备忘录
     * @param {*} param0
     */
    async add({
        platform,
        task,
        benjin,
        yongjin,
        is_benjin_return,
        is_yongjin_return,
        mark,
    }) {
        let sql =
            "insert into bwl (platform, task, benjin, yongjin, is_benjin_return, is_yongjin_return, mark, uid) values (?, ?, ?, ?, ?, ?, ?, ?)";
        let params = [
            platform,
            task,
            benjin,
            yongjin,
            is_benjin_return,
            is_yongjin_return,
            mark,
            this.ctx.user.id,
        ];
        let res = await this.app.mysql.query(sql, params);
        return res;
    }

    async readList() {
        let sql = "select * from bwl where uid = ?";
        let parmas = [this.ctx.user.id];
        let res = await this.app.mysql.query(sql, parmas);
        return res;
    }

    async readListPage(pagesize, pagenum) {
        // 计数
        let sql = "select count(*) as count from bwl where uid = ?";
        let parmas = [this.ctx.user.id];
        let res = await this.app.mysql.query(sql, parmas);
        const total = res[0]["count"];
        // 分页数据
        const startIndex = (pagenum - 1) * pagesize;
        sql = "select * from bwl where uid = ? limit ?, ?";
        parmas = [this.ctx.user.id, startIndex, pagesize];
        res = await this.app.mysql.query(sql, parmas);
        const list = res;
        return { total, list };
    }

    async find(id) {
        let sql = "select * from bwl where id = ?";
        let params = [id];
        let res = await this.app.mysql.query(sql, params);
        return res[0];
    }

    async update(id, is_benjin_return, is_yongjin_return) {
        let sql =
            "update bwl set is_benjin_return = ?, is_yongjin_return = ? where id = ?";
        let params = [is_benjin_return, is_yongjin_return, id];
        let res = await this.app.mysql.query(sql, params);
        return res;
    }

    async delete(id) {
        let sql = "delete from bwl where id = ?";
        let params = [id];
        let res = await this.app.mysql.query(sql, params);
        return res;
    }
}

module.exports = BwlService;
